package com.xuerhua.yxt.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.xuerhua.yxt.model.VisitorAppointment;
import com.xuerhua.yxt.reqVo.HkFaceReqVO;
import com.xuerhua.yxt.reqVo.VisitorAppointmentReqVO;

public class HkFaceUtil {
	
	private static Logger logger = LoggerFactory.getLogger(HkFaceUtil.class);
	
	private final static String ARTEMIS_PATH = "/artemis";
	private final static String CONTENT_TYPE = "application/json";
	
	private static void init() {
		ArtemisConfig.host = "ai.nhedu.net"; // artemis网关服务器ip端口
	    ArtemisConfig.appKey = "23944270"; // 秘钥appkey
	    ArtemisConfig.appSecret = "V0cxi00A9KA0BmP3tJzT";// 秘钥appSecret
	}
	/**
	 * 添加人脸组
	 * @param name
	 * @param description
	 * @return
	 */
	public static String addSingleFaceGroup(String name, String description) {
		init();
        String getApi = ARTEMIS_PATH + "/api/frs/v1/face/group/single/addition";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("name", name);
        jsonBody.put("description", description);
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
    }
	/**
	 * 查询人脸组信息
	 * @return
	 */
	public static String queryFaceGroup() {
		init();
        String getApi = ARTEMIS_PATH + "/api/frs/v1/face/group";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("name", "学生组");
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
    }
	/**
	 * 添加单个人脸
	 * @param vo
	 * @return
	 */
	public static String addSingleFace(HkFaceReqVO vo) {
		init();
        String getApi = ARTEMIS_PATH + "/api/frs/v1/face/single/addition";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        Map<String, String> faceInfo = new HashMap<String, String>(4);
        faceInfo.put("name", vo.getName());
        if(StringUtils.isNotBlank(vo.getSex())) {
        	faceInfo.put("sex", vo.getSex());
        }
    	faceInfo.put("certificateType", vo.getCertificateType());
    	faceInfo.put("certificateNum", vo.getCertificateNum());
        Map<String, String> facePic = new HashMap<String, String>(1);
        if(StringUtils.isNotBlank(vo.getFaceUrl())) {
        	facePic.put("faceUrl", vo.getFaceUrl());
        }
        if(StringUtils.isNotBlank(vo.getFaceBinaryData())) {
        	facePic.put("faceBinaryData", vo.getFaceBinaryData());
        }
        jsonBody.put("faceGroupIndexCode", vo.getFaceGroupCode());
        jsonBody.put("faceInfo", faceInfo);
        jsonBody.put("facePic", facePic);
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
    }

	public static String deleteSingleFace(String faceGroupCode, String faceIndexCode) {
		init();
		String getApi = ARTEMIS_PATH + "/api/frs/v1/face/deletion";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
		JSONObject jsonBody = new JSONObject();
		
//		JSONArray jsonArray = new JSONArray();
//		JSONObject json = new JSONObject();
//        List<String> list = new ArrayList<String>();
//        list.add(faceIndexCode);
//        json.put("indexCodes", list);
//        json.put("faceGroupIndexCode", faceGroupCode);
//        jsonArray.add(json);
//		  jsonBody.put("condition", jsonArray);
		
		List<String> list = new ArrayList<String>();
		list.add(faceIndexCode);
		jsonBody.put("indexCodes", list);
		jsonBody.put("faceGroupIndexCode", faceGroupCode);
		
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
	}
	
	public static String updateSingleFace(HkFaceReqVO vo) {
		init();
		String getApi = ARTEMIS_PATH + "/api/frs/v1/face/single/update";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        Map<String, String> faceInfo = new HashMap<String, String>(4);
        faceInfo.put("name", vo.getName());
        if(StringUtils.isNotBlank(vo.getSex())) {
        	faceInfo.put("sex", vo.getSex());
        }
        if(StringUtils.isNotBlank(vo.getCertificateType())) {
        	faceInfo.put("certificateType", vo.getCertificateType());
        }
        if(StringUtils.isNotBlank(vo.getCertificateNum())) {
        	faceInfo.put("certificateNum", vo.getCertificateNum());
        }
        Map<String, String> facePic = new HashMap<String, String>(1);
        if(StringUtils.isNotBlank(vo.getFaceUrl())) {
        	facePic.put("faceUrl", vo.getFaceUrl());
        }
        if(StringUtils.isNotBlank(vo.getFaceBinaryData())) {
        	facePic.put("faceBinaryData", vo.getFaceBinaryData());
        }
        jsonBody.put("indexCode", vo.getFaceIndexCode());
        jsonBody.put("faceInfo", faceInfo);
        jsonBody.put("facePic", facePic);
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
    }
	
	
	
	public static String addSingleFace() {
		init();
        String getApi = ARTEMIS_PATH + "/api/frs/v1/face/single/addition";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        Map<String, String> faceInfo = new HashMap<String, String>(4);
        faceInfo.put("name", "云校通测试");
        faceInfo.put("sex", "1");
        faceInfo.put("certificateType", "113");
        faceInfo.put("certificateNum", "42900419906274911");
        Map<String, String> facePic = new HashMap<String, String>(1);
        facePic.put("faceUrl", "http://peili.xuerhua.com/smfx/images/404.jpg");
        //facePic.put("faceBinaryData", AipFaceClient.ImageToBase64ByOnline("http://peili.xuerhua.com/smfx/images/404.jpg"));
        jsonBody.put("faceGroupIndexCode", "8b69b335-2a20-4afa-8e88-5d7eb984d1c9");
        jsonBody.put("faceInfo", faceInfo);
        jsonBody.put("facePic", facePic);
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
    }
	
	/**
	 *  5.4.5.5	获取人员列表
	 *
	 * @return
	 */
	public static String getPersonInfosByPage(int pageNo, int pageSize) {
		init();
        String getApi = ARTEMIS_PATH + "/api/resource/v1/person/advance/personList";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNo);  
        jsonBody.put("pageSize", pageSize); 
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
        logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
	}
	
	public static String VisitorAppointment(VisitorAppointmentReqVO vo) {
		init();
		String getApi = ARTEMIS_PATH + "/api/visitor/v1/appointment";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        JSONObject jsonBody = new JSONObject();
    	jsonBody.put("receptionistId", vo.getReceptionistId());
    	jsonBody.put("visitStartTime", DateUtil.dateToStr(vo.getVisitStartTime(), DateUtil.DEFAULT_DATE_FORMAT5));
    	jsonBody.put("visitEndTime", DateUtil.dateToStr(vo.getVisitEndTime(), DateUtil.DEFAULT_DATE_FORMAT5));
    	jsonBody.put("visitPurpose", vo.getVisitPurpose());
    	
    	List<VisitorAppointment> visitorInfoList = new ArrayList<VisitorAppointment>();
    	VisitorAppointment visitorInfo = new VisitorAppointment();
    	visitorInfo.setVisitorName(vo.getVisitorName());
    	visitorInfo.setGender(vo.getGender());
    	visitorInfo.setPhoneNo(vo.getPhoneNo());
    	visitorInfoList.add(visitorInfo);
    	jsonBody.put("visitorInfoList", visitorInfoList);
    	String body = jsonBody.toJSONString();
    	
    	String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
    	System.out.println("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
	}
	
	public static String addPerson(HkFaceReqVO hkVO) {
		init();
		String getApi = ARTEMIS_PATH + "/api/resource/v1/person/batchadd";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getApi);//根据现场环境部署确认是http还是https
            }
        };
        JSONArray jsonArray = new JSONArray();
		JSONObject json = new JSONObject();
        json.put("personName", hkVO.getName());
        json.put("sex", hkVO.getSex());
        json.put("orgIndexCode", hkVO.getFaceGroupCode());
        json.put("jobNo", hkVO.getCertificateNum());
        jsonArray.add(json);
    	String body = jsonArray.toJSONString();
    	
    	String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE);
    	logger.info("请求路径："+ getApi + ",请求参数："+ body + ",返回结果：" + result);
        return result;
	}
	
    public static void main(String[] args) {
        //addSingleFaceGroup("教师组","学校教师组");
    	//queryFaceGroup();
    	//addSingleFace();
    	//addSingleFace();
    	try {
    		//addSingleFace();
    		getPersonInfosByPage(1, 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }

}
